/**
 * Created by Myron on 2015/8/21.
 */
//AngularJS 控制器

grApp.controller('SginDayAngularCtrl', ['$scope',
    '$http', 'SginDayFactory',
    function ($scope, $http, sginDayFactory) {

        var $modalconfirm =angular.element("#my-confirm");

        //变量初始化
        $scope.MonthSignDay; //月度签到天数
        $scope.AllSignDay; //累计签到天数

        $scope.GiftArray = [];
        $scope.load = function () {
            //页面获取数据
            sginDayFactory.getSginDayValue().then(function (result) {
                    $scope.MonthSignDay = result.MonthSignDay;
                    $scope.AllSignDay = result.AllSignDay;
                    $scope.SignState = result.SignState;
                    $scope.GiftArray = result.GiftArray;
                    //alert($scope.SignState);
            })
        };
        $scope.load();
        //签到按钮点击事件
        $scope.signBtn = function () {
            if ($scope.SignState == 0) {
                sginDayFactory.setSginDayAction().then(function (result) {
                    if (result.code == 0) {
                        AlertShow("提醒","签到成功",true);
                    } else {
                        if(result.code == 1011){
                            AlertShow("提醒","你今天已经签到过了哦!",false);
                        }else{
                            AlertShow("提醒","签到失败.页面正在维护中."+result.code,true);
                        }
                    }
                })
            }else{
                AlertShow("提醒","你今天已经签到过了哦!",false);
            }
        };

        //礼物领取按钮
        $scope.giveBtn=function(RuleDays,GaveValue){
            switch(RuleDays)
            {
                case 3:
                    if($scope.GiftArray[0].D3Value==-1){ AlertShow("活动规则","您当月已经连续签到"+ $scope.MonthSignDay+"天。还差"+(3- $scope.MonthSignDay)+"天即可获得「5元优惠券」一张。优惠券可以结账时使用哦！",false)};
                    if($scope.GiftArray[0].D3Value==0){
                        GiveAction($scope.GiftArray[0].D3ObjectId,3,"奖品领取","5元优惠券可以结账时抵用5元现金！","恭喜您，奖品领取成功！「5元优惠券」已经存入您的账号。前往 个人中心->我的优惠券 中可以查看，优惠券有效期7天。记得使用哦！");
                    }
                    break;
                case 15:
                    if($scope.GiftArray[1].D15Value==-1){ AlertShow("活动规则","您当月已经连续签到"+ $scope.MonthSignDay+"天。还差"+(15- $scope.MonthSignDay)+"天即可获得「价值20元水果」一份。",false)};
                    if($scope.GiftArray[1].D15Value==0){
                        GiveAction($scope.GiftArray[1].D15ObjectId,15,"奖品领取","可获得「价值20元水果」一份！","恭喜您，奖品领取成功！「价值20元水果」已经放入购物车！记得前往购物车结账带回家哦！");
                    }
                    break;
                case 30:
                    var d= new Date();
                    var md= new Date(d.getFullYear(), d.getMonth()+1,0).getDate();
                    if($scope.GiftArray[2].D30Value==-1){ AlertShow("活动规则","您当月已经连续签到"+ $scope.MonthSignDay+"天。还差"+(md- $scope.MonthSignDay)+"天即可获得「10元优惠券」一张。优惠券可以结账时使用哦！",false)};
                    if($scope.GiftArray[2].D30Value==0){
                        GiveAction($scope.GiftArray[2].D30ObjectId,30,"奖品领取","10元优惠券可以结账时抵用10元现金！","恭喜您，奖品领取成功！「10元优惠券」已经存入您的账号。前往 个人中心->我的优惠券 中可以查看，优惠券有效期7天。记得使用哦！");
                    }
                    break;
                case 90:
                    if($scope.GiftArray[3].D90Value==-1){ AlertShow("活动规则","您已经连续签到"+ $scope.AllSignDay+"天。还差"+(90- $scope.AllSignDay)+"天即可获得「价值100元水果」一份。",false)};
                    if($scope.GiftArray[3].D90Value==0){
                        GiveAction($scope.GiftArray[3].D90ObjectId,90,"奖品领取","可获得「价值100元水果」一份！","恭喜您，奖品领取成功！「价值100元水果」已经放入购物车！记得前往购物车结账带回家哦！");
                    }
                    break;
                case 180:
                    if($scope.GiftArray[4].D180Value==-1){ AlertShow("活动规则","您已经连续签到"+ $scope.AllSignDay+"天。还差"+(180- $scope.AllSignDay)+"天即可获得「500元现金充值」。",false)};
                    if($scope.GiftArray[4].D180Value==0){
                        GiveAction($scope.GiftArray[4].D180ObjectId,180,"奖品领取","可获得「500元现金充值」购物爽翻天！","恭喜您，奖品领取成功！「500元现金」已经存入您的账号，赶快去买水果吧！");
                    }
                    break;
                case 365:
                    if($scope.GiftArray[5].D365Value==-1){ AlertShow("活动规则","您已经连续签到"+ $scope.AllSignDay+"天。还差"+(365- $scope.AllSignDay)+"天即可获得「2000元现金充值」。",false)};
                    if($scope.GiftArray[5].D365Value==0){
                        GiveAction($scope.GiftArray[5].D365ObjectId,365,"奖品领取","可获得「2000元现金充值」终极大奖！","恭喜您，奖品领取成功！「2000元现金」已经存入您的账号，赶快去买水果吧！");
                    }
                    break;
                default:
                    //都不执行时的代码块

            }

        }

        //执行领取方法
        var GiveAction=function(GiftGiveID,RuleDays,title,content,successcontent){
            $scope.ConfirmTitle=title;
            $scope.ConfirmContent=content;
            $scope.GiftGiveID=GiftGiveID;
            $scope.RuleDays=RuleDays;

            $scope.SuccessContent=successcontent;
            $modalconfirm.modal({
                relatedTarget: this,
                onConfirm: function(e) {
                    sginDayFactory.setSginDayGiftGive($scope.GiftGiveID,$scope.RuleDays).then(function(result){
                        //alert(result.code+result.msg);
                        if(result.code==0){
                            AlertShow("领取成功", $scope.SuccessContent,true);
                        }else{
                            AlertShow("领取失败","领取失败."+result.msg,true);
                        }

                    })
                },
                onCancel: function(e) {

                }
            });

        }

        //alert模态框调用
        var AlertShow= function (title,content,reload) {
            var $modalalert =angular.element("#my-alert");
            $scope.AlertTitle=title;
            $scope.AlertContent=content;
            $scope.reload=reload;
            $modalalert.modal({
                relatedTarget: this,
                onCancel: function(e) {
                    if($scope.reload){
                        location.reload();
                    }
                }
            });
        }

        //Confirm模态框复制用
     /*   $scope.ConfirmTitle=title;
        $scope.ConfirmContent=content;
        $modalconfirm.modal({
            relatedTarget: this,
            onConfirm: function(e) {
                alert('你输入的是：' + e.data || '')
            },
            onCancel: function(e) {
                alert('不想说!');
            }
        });*/

    }]);

//签到工厂
grApp.factory('SginDayFactory', ['$http', '$q', function ($http, $q) {
    var service = {};
    //进入页面获取数据
    service.getSginDayValue = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/getSignDayValueAjax'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //签到Ajax操作
    service.setSginDayAction = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/setSignDayActionAjax'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //领取奖品操作
    service.setSginDayGiftGive=function(GiftGiveID,RuleDays){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/setSignDayGiftGiveAjax',
            params: {GiftGiveID: GiftGiveID,RuleDays:RuleDays}
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    }
    return service;

}]);
